/**业务管理流程平台版本1.0,生成时间:2017-04-13 13:16:48**/
package com.xbdun.dao.bmp.provider;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

/**
 * 动态XbdunSysUserDo的SQL语句提供者
 * 平台用户信息
 * @author 张国涛
 * @2017-04-13 13:16:48
 */
public class SysUserSqlProvider {

	//日志
	Log log = LogFactory.getLog(SysUserSqlProvider.class);
	
	/**
	 * 动态sql更新对象
	 * @param map
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String updateDynXbdunSysUserSql(Map<String,Object> map){
		
		StringBuilder sbSql = new StringBuilder();
		Map<String,Object> params = (Map<String, Object>) map.get("param");
		String _where = constWhere(map);
		boolean bl = false;
		
		Assert.isTrue(StringUtils.isNotEmpty(_where),"动态更新操作，条件Where不存在");
		
		//移除Where条件
		sbSql.append("UPDATE xbdun_sys_user SET ");
		if(!params.isEmpty()){
			Iterator<Entry<String, Object>> iparam = params.entrySet().iterator();
			while(iparam.hasNext()){
				if(bl)sbSql.append(",");
				
				Entry<String, Object> enty = iparam.next();
				sbSql.append(enty.getKey()+" = "+"#{param."+enty.getKey()+"}");
				
				if(!bl)bl = true;
			}
			//设置Where条件
			if(!StringUtils.isEmpty(_where)){
				sbSql.append(" WHERE "+_where);
			}
		}else{
			//参数为空则，清空所有的SQL
			sbSql.setLength(0);
		}
		String sqlContent = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return sqlContent;
	}

	/**
	 * 判断是否存在,不为null则是存在
	 * @param map
	 */
	public String existSql(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		@SuppressWarnings("unchecked")
		Map<String,Object> mparam = (Map<String,Object>)params.get("param");
		boolean bl = false;
		sbSql.append("SELECT COUNT(*) FROM xbdun_sys_user WHERE ");
		if(!params.isEmpty()){
			Iterator<Entry<String, Object>> iparam = mparam.entrySet().iterator();
			while(iparam.hasNext()){
				
				Entry<String, Object> enty = iparam.next();
				if(bl){sbSql.append(" AND ");}
				
				sbSql.append(enty.getKey()+" = "+"#{param."+enty.getKey()+"}");
				
				if(!bl)bl = true;
			}
		}
		
		String content = sbSql.toString();
		sbSql.setLength(0);
		sbSql = null;
		
		return content;
	}
	/**
	 * 查询分页
	 * @param params
	 * @return
	 */
	public String queryPagerSql(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		//排序规则
		String orderBy = (String) params.get("_orderby");
		//当前页
		int currentIndex = (Integer) params.get("pageNum");
		//显示条数
		int numPerPage = (Integer) params.get("numPerPage");

		sbSql.append("SELECT * ");
		sbSql.append(" FROM xbdun_sys_user");
		String where = constWhere(params);
		if(!StringUtils.isEmpty(where)){
			sbSql.append(" WHERE ");
			sbSql.append(where);
		}
		
		//查看排序
		if(!StringUtils.isEmpty(orderBy)){
			sbSql.append(" ORDER BY "+orderBy);
		}
		
		//分页
		if(numPerPage>0){
			sbSql.append(" LIMIT "+currentIndex+","+numPerPage);
		}
		
		String contentSql = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
	}
	
	//生成查询分页where条件
	private String constWhere(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		//where查询条件
		@SuppressWarnings("unchecked")
		Map<String,Object> _where = (Map<String,Object>) params.get("_where");
		if(!CollectionUtils.isEmpty(_where)){
			Iterator<Entry<String, Object>> iparam = _where.entrySet().iterator();
			boolean bl = false;
			while(iparam.hasNext()){
				Entry<String, Object> enty = iparam.next();
				//选择值跳出继续
				if(enty.getKey().equals("selectValue"))continue;
				if(bl){sbSql.append(" AND ");}
				//如果是下拉选择
				if(enty.getKey().equals("selectKey")){
					//下拉选项 员工真实姓名【1】，电话【2】，工号【3】，机构邮箱【4】
					switch((Integer)enty.getValue()){
						case 1:
							sbSql.append("realName LIKE CONCAT('%',#{_where.selectValue},'%')");
							break;
						case 2:
							sbSql.append("mobile = #{_where.selectValue}");
							break;
						case 3:
							sbSql.append("jonNum = #{_where.selectValue}");
							break;
						case 4:
							sbSql.append("idCard = #{_where.selectValue}");
							break;
						default:
							sbSql.append(" 1 = 1");
					}
					if(!bl)bl = true;
					continue;
				}
				sbSql.append(enty.getKey()+" = "+"#{_where."+enty.getKey()+"}");
				if(!bl)bl = true;
			}
		}
		
		String contentSql = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
	}
	
	/**
	 * 新增查询分页查询总条数
	 * @param params
	 * @return
	 */
	public String queryPagerCountSql(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		
		sbSql.append("SELECT COUNT(*) ");
		sbSql.append(" FROM xbdun_sys_user");
		String where = constWhere(params);
		if(!StringUtils.isEmpty(where)){
			sbSql.append(" WHERE ");
			sbSql.append(where);
		}
		String contentSql = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
		
	}
	
	/**
	 * 查询分页
	 * @param params
	 * @return
	 */
	public String querySysUserPagerSql(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		//排序规则
		String orderBy = (String) params.get("_orderby");
		//当前页
		int pageIndex = (Integer) params.get("pageIndex");
		//显示条数
		int pageSize = (Integer) params.get("pageSize");
		
		sbSql.append("SELECT * ");
		sbSql.append(" FROM xbdun_sys_user");
		String where = constWhere(params);
		if(!StringUtils.isEmpty(where)){
			sbSql.append(" WHERE ");
			sbSql.append(where);
		}
		
		//查看排序
		if(!StringUtils.isEmpty(orderBy)){
			sbSql.append(" ORDER BY "+orderBy);
		}
		
		//分页
		if(pageSize>0){
			sbSql.append(" LIMIT "+pageIndex+","+pageSize);
		}
		
		String contentSql = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
	}
	
	/**
	 * 机构+平台查询分页查询总条数
	 * @param params
	 * @return
	 */
	public String queryOrgUserPagerCountSql(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		
		sbSql.append("SELECT COUNT(*) ");
		sbSql.append(" FROM xbdun_org_user a LEFT JOIN xbdun_sys_user b ON a.orgCode = b.orgCode ");
		String where = constOrgUserWhere(params);
		if(!StringUtils.isEmpty(where)){
			sbSql.append(" WHERE ");
			sbSql.append(where);
		}
		String contentSql = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
		
	}

	private String constOrgUserWhere(Map<String, Object> params) {
		StringBuilder sbSql = new StringBuilder();
		//where查询条件
		@SuppressWarnings("unchecked")
		Map<String,Object> _where = (Map<String,Object>) params.get("_where");
		if(!CollectionUtils.isEmpty(_where)){
			Iterator<Entry<String, Object>> iparam = _where.entrySet().iterator();
			boolean bl = false;
			while(iparam.hasNext()){
				Entry<String, Object> enty = iparam.next();
				//not_specilRole
				//选择值跳出继续
				if(enty.getKey().equals("selectValue"))continue;
				if(bl){sbSql.append(" AND ");}
				//如果是下拉选择
				if(enty.getKey().equals("selectKey")){
					//下拉选项 员工真实姓名【1】，电话【2】，工号【3】，机构邮箱【4】
					switch((Integer)enty.getValue()){
						case 1:
							sbSql.append("a.realName LIKE CONCAT('%',#{_where.selectValue},'%')");
							break;
						case 2:
							sbSql.append("a.mobile = #{_where.selectValue}");
							break;
						case 3:
							sbSql.append("a.jonNum = #{_where.selectValue}");
							break;
						case 4:
							sbSql.append("b.idCard = #{_where.selectValue}");
							break;
						default:
							sbSql.append(" 1 = 1");
					}
					if(!bl)bl = true;
					continue;
				}
				sbSql.append("a."+enty.getKey()+" = "+"#{_where."+enty.getKey()+"}");
				if(!bl)bl = true;
			}
		}
		
		String contentSql = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
	}
	
	/**
	 * 查询分页
	 * @param params
	 * @return
	 */
	public String queryOrgUserPagerSql(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		//排序规则
		String orderBy = (String) params.get("_orderby");
		//当前页
		int pageIndex = (Integer) params.get("pageIndex");
		//显示条数
		int pageSize = (Integer) params.get("pageSize");
		
		sbSql.append("SELECT * ");
		sbSql.append(" FROM xbdun_org_user a LEFT JOIN xbdun_sys_user b ON a.orgCode = b.orgCode ");
		String where = constOrgUserWhere(params);
		if(!StringUtils.isEmpty(where)){
			sbSql.append(" WHERE ");
			sbSql.append(where);
		}
		
		//查看排序
		if(!StringUtils.isEmpty(orderBy)){
			sbSql.append(" ORDER BY a."+orderBy);
		}
		
		//分页
		if(pageSize>0){
			sbSql.append(" LIMIT "+pageIndex+","+pageSize);
		}
		
		String contentSql = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
	}
}

